package org.comet.auxiliary.client.swing;

import org.comet.auxiliary.common.RelationInfo;
import org.comet.auxiliary.common.UserInfo;
import org.comet.auxiliary.common.swing.NotEditableTable;
import org.comet.auxiliary.common.swing.SwingHelper;
import org.comet.auxiliary.common.util.StringUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;

/**
 * @author wq
 * @date 2019-10-24
 * @description palm-client
 */
public class FriendListFrame extends JDialog implements ActionListener {
    private String title;
    private String idObj = "";
    // "私聊发包", "群聊发包"
    private String idType = "群聊发包";
    private String idName ="";
    private JTextField searchFriendText;
    private JTable friendGrid;
    private JTextField searchRelationText;
    private JTable relationGrid;

    private List<Map<String, Object>> friendInfo;
    private List<Map<String, Object>> relationInfo;

    public FriendListFrame(Frame owner, String title, int width, int height) {
        super(owner, true);
        this.title = title;
        setBounds(700, 300, width, height);
    }

    public static void main(String[] args) {
        SwingHelper.setCustomFont(new Font("宋体", Font.PLAIN, 10));
        FriendListFrame friendListFrame = new FriendListFrame(null, "nihao", 453, 315);
//        friendListFrame.openFriendList(null, null, false);
    }

    public void openFriendList(List<Map<String,Object>> friendInfo, List<Map<String,Object>> relationInfo) {
        setTitle("选择对象(可选中对象后 Ctrl+C 复制行数据, 即可获取 ID 信息)");

        setLayout(new GridLayout(1, 2, 2, 1));

        JLabel searchFriendLabel = new JLabel("查询好友:");

        searchFriendText = new JTextField(6);
        JButton searchBtn = new JButton("查询");
        searchBtn.setActionCommand("queryFriend");
        searchBtn.addActionListener(this);

        JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        searchPanel.add(searchFriendLabel);
        searchPanel.add(searchFriendText);
        searchPanel.add(searchBtn);


        friendGrid = new NotEditableTable(0, 2);
        JScrollPane friendGridScroll = new JScrollPane(friendGrid);

        JPanel friendGridPanel = new JPanel(new BorderLayout());
        friendGridPanel.add(friendGridScroll);

        JButton confirmBackBtn = new JButton("确认选择");
        confirmBackBtn.setActionCommand("confirmFriend");
        confirmBackBtn.addActionListener(this);

        JPanel friendFooter = new JPanel(new FlowLayout(FlowLayout.CENTER));
        friendFooter.add(confirmBackBtn);

        JPanel friend = new JPanel(new BorderLayout(0, 3));
        friend.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        friend.add(searchPanel, BorderLayout.NORTH);
        friend.add(friendGridPanel, BorderLayout.CENTER);
        friend.add(friendFooter, BorderLayout.SOUTH);

        JLabel searchRelationLabel = new JLabel("查询群:");

        searchRelationText = new JTextField(6);
        JButton searchRelationBtn = new JButton("查询");
        searchRelationBtn.setActionCommand("queryRelation");
        searchRelationBtn.addActionListener(this);

        JPanel searchRelationPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        searchRelationPanel.add(searchRelationLabel);
        searchRelationPanel.add(searchRelationText);
        searchRelationPanel.add(searchRelationBtn);

        relationGrid = new NotEditableTable(0, 3);
        JScrollPane relationGridScroll = new JScrollPane(relationGrid);

        JPanel relationGridPanel = new JPanel(new BorderLayout());
        relationGridPanel.add(relationGridScroll);

        JButton confirmRelationBtn = new JButton("确认选择");
        confirmRelationBtn.setActionCommand("confirmRelation");
        confirmRelationBtn.addActionListener(this);

        JPanel footer = new JPanel(new FlowLayout(FlowLayout.CENTER));
        footer.add(confirmRelationBtn);

        JPanel relation = new JPanel(new BorderLayout(0, 3));
        relation.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        relation.add(searchRelationPanel, BorderLayout.NORTH);
        relation.add(relationGridPanel, BorderLayout.CENTER);
        relation.add(footer, BorderLayout.SOUTH);

        Container container = getContentPane();
        container.add(relation);
        container.add(friend);

        this.friendInfo = friendInfo;
        this.relationInfo = relationInfo;
        initFriendGrid("");
        initRelationGrid("");

        setVisible(true);
    }

    /**
     * 渲染表格, 并根据搜索条件过滤
     */
    private void initFriendGrid(String friendSearch) {
        DefaultTableModel friendGridModel = (DefaultTableModel) friendGrid.getModel();
        friendGridModel.setRowCount(0);
        friendGridModel.setDataVector(new Object[][]{}, new Object[]{"用户 ID", "用户昵称"});

        if (friendInfo == null) {
            return;
        }
//        for (UserInfo friend : friendInfo.values()) {
//            if (friend.getUid().length() > 3) {
//                if (StringUtil.isBlank(friendSearch) || friend.getUnk().contains(friendSearch)) {
//                    friendGridModel.addRow(new Object[]{friend.getUid(), friend.getUnk()});
//                }
//            }
//        }
        for (Map<String, Object> map : friendInfo) {
            if(map.get("uid").toString().length()>3){
                if (StringUtil.isBlank(friendSearch) || map.get("unk").toString().contains(friendSearch)) {
                    friendGridModel.addRow(new Object[]{map.get("uid").toString(), map.get("unk").toString()});
                }
            }
        }



        friendGrid.setRowHeight(22);
        friendGrid.setModel(friendGridModel);
        if (friendGrid.getRowCount() > 0) {
            friendGrid.setRowSelectionInterval(0, 0);
        }
    }

    /**
     * 渲染表格, 并根据搜索条件过滤
     */
    private void initRelationGrid(String relationSearch) {
        DefaultTableModel relationGridModel = (DefaultTableModel) relationGrid.getModel();
        relationGridModel.setDataVector(new Object[][]{}, new Object[]{"群 ID", "群名", "群人数"});

        if (relationInfo == null) {
            return;
        }
//        for (RelationInfo relation : relationInfo.values()) {
//            if (StringUtil.isBlank(relationSearch) || relation.getRn().contains(relationSearch)) {
//                relationGridModel.addRow(new Object[]{relation.getRid(), relation.getRn(), relation.getJmt()});
//            }
//        }
        for (Map<String, Object> map : relationInfo) {
            if (StringUtil.isBlank(relationSearch) || map.get("rn").toString().contains(relationSearch)) {
                relationGridModel.addRow(new Object[]{ map.get("rid").toString(),  map.get("rn").toString(),map.get("jmt").toString()});
            }
        }

        relationGrid.setRowHeight(22);
        relationGrid.setModel(relationGridModel);
        if (relationGrid.getRowCount() > 0) {
            relationGrid.setRowSelectionInterval(0, 0);
        }
    }

    public String getIdObj() {
        return idObj;
    }

    public String getIdType() {
        return idType;
    }

    public String getIdName(){
        return idName;
    }



    /**
     * 根据按钮赋值选择的发包对象
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if ("queryFriend".equals(actionCommand)) {
            String search = searchFriendText.getText();
            initFriendGrid(search);
        } else if ("queryRelation".equals(actionCommand)) {
            String search = searchRelationText.getText();
            initRelationGrid(search);
        } else if ("confirmFriend".equals(actionCommand)) {
            idType = "私聊发包";
            int selectedRow = friendGrid.getSelectedRow();
            if (selectedRow != -1) {
                TableModel friendGridModel = friendGrid.getModel();
                Object value = friendGridModel.getValueAt(selectedRow, 0);
                idObj = (String) value;
                this.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(this, "请先选择一个联系人", title, JOptionPane.ERROR_MESSAGE);
            }
        } else if ("confirmRelation".equals(actionCommand)) {
            idType = "群聊发包";
            int selectedRow = relationGrid.getSelectedRow();
            if (selectedRow != -1) {
                TableModel relationGridModel = relationGrid.getModel();
                Object value = relationGridModel.getValueAt(selectedRow, 0);
                Object name = relationGridModel.getValueAt(selectedRow, 1);
                idObj = (String) value;
                idName = (String) name;
                this.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(this, "请先选择一个群聊", title, JOptionPane.ERROR_MESSAGE);
            }
        }
    }

}
